package dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.mybatis.spring.SqlSessionTemplate;

import dao.TeamManagerDao;
import pojo.Projects;
import pojo.Teams;
import pojo.UserRoleProject;
import pojo.UserTeam;
import pojo.Users;



public class TeamManagerDaoImpl implements TeamManagerDao{
	private SqlSessionTemplate sqlSessionTemplate;
	public SqlSessionTemplate getSqlSessionTemplate() {
		return sqlSessionTemplate;
	}

	public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
		this.sqlSessionTemplate = sqlSessionTemplate;
	}
	
	
	@Override
	public List<Map<String,String>> getApplyUsers1ByTeamid(int teamid) {
		// TODO Auto-generated method stub
		return sqlSessionTemplate.selectList("getApplyUsers1ByTeamid", teamid);
	}
	@Override
	public List<Map<String, String>> getPartApplyUsersByTeamid(int teamid) {
		// TODO Auto-generated method stub
		return sqlSessionTemplate.selectList("getPartApplyUsersByTeamid", teamid);
	}
	@Override
	public List<Map<String,String>> getApplyUsers2ByTeamid(int teamid) {
		// TODO Auto-generated method stub
		return sqlSessionTemplate.selectList("getApplyUsers2ByTeamid", teamid);
	}
	@Override
	public List<Map<String, String>> getPart2ApplyUsersByTeamid(int teamid) {
		// TODO Auto-generated method stub
		return sqlSessionTemplate.selectList("getPart2ApplyUsersByTeamid", teamid);
	}
	
	@Override
	public Users getUserByUserid(int userid) {
		// TODO Auto-generated method stub
		return (Users)sqlSessionTemplate.selectOne("getUserByUserid", userid);
	}
	
	@Override
	public int updateUserTeamIsAccepted(int userid,int teamid) {
		// TODO Auto-generated method stub
		Object object=new Object[]{userid,teamid};
		return sqlSessionTemplate.update("updateUserTeamIsAccepted",object);
	}
	@Override
	public List<Map<String,String>> appluUserFenYePrev(int applyNowNo, int teamid) {
		// TODO Auto-generated method stub
		Object object=new Object[]{teamid,(applyNowNo-1)*4};
		return sqlSessionTemplate.selectList("appluUserFenYePrev", object);
	}

	@Override
	public List<Map<String,String>> appluUserFenYeNext(int applyNowNo, int teamid) {
		// TODO Auto-generated method stub
		Object object=new Object[]{teamid,(applyNowNo-1)*4};
		return sqlSessionTemplate.selectList("appluUserFenYeNext", object);
	}
	@Override
	public List<Map<String, String>> getCreateUserByTeamid(int teamid) {
		// TODO Auto-generated method stub
		return sqlSessionTemplate.selectList("getCreateUserByTeamid", teamid);
	}
	@Override
	public List<Map<String, String>> getAllTeamByUserid(int userid) {
		// TODO Auto-generated method stub
		return sqlSessionTemplate.selectList("getAllTeamByUserid", userid);
	}

	@Override
	public List<Users> getAllTeamUserByTeamid(int teamid) {
		// TODO Auto-generated method stub
		return sqlSessionTemplate.selectList("getAllTeamUserByTeamid", teamid);
	}

	@Override
	public List<Users> getPartTeamUserByTeamid(int teamid) {
		// TODO Auto-generated method stub
		return sqlSessionTemplate.selectList("getPartTeamUserByTeamid", teamid);
	}

	@Override
	public List<Users> teamUserFenYe(int teamUserNowNo, int teamid) {
		// TODO Auto-generated method stub
		Object object=new Object[]{teamid,(teamUserNowNo-1)*4};
		return sqlSessionTemplate.selectList("teamUserFenYe", object);
	}

	@Override
	public int deleteTeamMember(int userid, int teamid) {
		// TODO Auto-generated method stub
		int b=0;
		//int a= deleteTeamMemberThenURP(userid);
	
		Object object=new Object[]{userid,teamid};
		b= sqlSessionTemplate.delete("deleteTeamMember",object);
		
		return b;
	}

	public int deleteTeamMemberThenURP(int userid){
		return sqlSessionTemplate.delete("deleteTeamMemberThenURP",userid);
	}
	
	
	
	
	@Override
	public int addProject(Projects p) {
		// TODO Auto-generated method stub
		return sqlSessionTemplate.insert("addProject",p);
	}

	@Override
	public int addProjectMember(UserRoleProject urp) {
		// TODO Auto-generated method stub
		return sqlSessionTemplate.insert("addProjectMember", urp);
	}

	@Override
	public List<Projects> getAllProjectsByTeamid(int teamid) {
		// TODO Auto-generated method stub
		return sqlSessionTemplate.selectList("getAllProjectsByTeamid", teamid);
	}

	@Override
	public List<Map<String, String>> getProjectMember(int projectid) {
		// TODO Auto-generated method stub
		return sqlSessionTemplate.selectList("getProjectMember",projectid);
	}



	@Override
	public int deleteProjectMember(int urpid) {
		// TODO Auto-generated method stub
		return sqlSessionTemplate.delete("deleteProjectMember", urpid);
	}

	@Override
	public int updateProjectMemberMTP(int projectid, int roleid, int urpid) {
		// TODO Auto-generated method stub
		Object object=new Object[]{projectid,roleid,urpid};
		return sqlSessionTemplate.update("updateProjectMemberMTP", object);
	}

	@Override
	public int updateProjectMemberPTM(int projectid, int roleid, int urpid) {
		// TODO Auto-generated method stub
		Object object=new Object[]{projectid,roleid,urpid};
		return sqlSessionTemplate.update("updateProjectMemberPTM", object);
	}

	@Override
	public int updatePrevPTM(int projectid, int roleid, int urpid) {
		// TODO Auto-generated method stub
		Object object=new Object[]{projectid,roleid,urpid};
		return sqlSessionTemplate.update("updatePrevPTM", object);
	}



	

	//XUYNJIN

	@Override
	public List<Map<String,Object>> getTeamByName(String teamName) throws Exception {
		// TODO Auto-generated method stub
		System.out.println(teamName);
		return sqlSessionTemplate.selectList("getTeamByName", "%"+teamName+"%");
	
	}
	
	@Override
	public List<Map<String, Object>> gotoTeamList(int userId) throws Exception {
		// TODO Auto-generated method stub
		System.out.println(userId);
		return sqlSessionTemplate.selectList("gotoTeamList", userId);
	}
	
	@Override
	public int addTeam(Teams teams) throws Exception {
		// TODO Auto-generated method stub
		return sqlSessionTemplate.insert("addTeam", teams);
	}
	
	@Override
	public Teams selectTeam(String teamName) throws Exception {
		// TODO Auto-generated method stub
		return (Teams)sqlSessionTemplate.selectOne("selectTeam",teamName);
	}
	
	@Override
	public int addUserTeam(UserTeam userteam) throws Exception {
		// TODO Auto-generated method stub
		return sqlSessionTemplate.insert("addUserTeam",userteam);
	}
	
	@Override
	public int updateUsers(Users users) throws Exception {
		// TODO Auto-generated method stub
		return sqlSessionTemplate.update("updateUsers",users);
	}

	@Override
	public int updateUserp(Users users) throws Exception {
		// TODO Auto-generated method stub
		return sqlSessionTemplate.update("updateUserp",users);
	}
	
	@Override
	public List<Map<String, Object>> getTeams(int currentPage, int pageSize) {
		// TODO Auto-generated method stub
		 System.out.println("third"+currentPage);
		 System.out.println("third"+pageSize);
		 int a=(currentPage-1)*pageSize;
		 System.out.println("a"+a);
		 
		 Map<String,Integer> map=new HashMap<>();
		 map.put("a", a);
		 map.put("pageSize", pageSize);
	//	 map.put("1",a);
	//	 map.put("2", pageSize);
		return sqlSessionTemplate.selectList("getTeams",map);
		}

//	@Override
//	public int count() {
//		// TODO Auto-generated method stub
//		return 0;
//	}

	
	@Override
	public List<Map<String, Object>> getTeam() throws Exception {
		// TODO Auto-generated method stub
		List<Map<String, Object>> list=sqlSessionTemplate.selectList("getTeam");
		return list;
	}

	@Override
	public Projects getProjectByProjectid(int projectid) {
		// TODO Auto-generated method stub
		return sqlSessionTemplate.selectOne("getProjectByProjectid",projectid);
	}

	@Override
	public int deleteProjectByProjectid(int projectid) {
		// TODO Auto-generated method stub
		return sqlSessionTemplate.delete("deleteProjectByProjectid",projectid);
	}

	

	@Override
	public int updateProjectInfoByProjectid(Projects p) {
		// TODO Auto-generated method stub
		
		return sqlSessionTemplate.update("updateProjectInfoByProjectid",p);
	}
	
	@Override
	public int addApplyTeam(UserTeam ut) {
		// TODO Auto-generated method stub
		
		return sqlSessionTemplate.insert("addApplyTeam", ut);
	}

	@Override
	public UserTeam checkIsApplied(int teamid, int userid) {
		// TODO Auto-generated method stub
		Object object=new Object[]{teamid,userid};
		return sqlSessionTemplate.selectOne("checkIsApplied", object);
	}

//	@Override
//	public List<Map<String, Object>> getTeamsec(String teamName, int currentPage, int pageSize) {
//		// TODO Auto-generated method stub
//		 System.out.println("third"+currentPage);
//		 System.out.println("third"+pageSize);
//		 int a=(currentPage-1)*pageSize;
//		 System.out.println("a"+a);
//		 
//		 Map<String,Object> map=new HashMap<>();
//		 map.put("a", a);
//		 map.put("pageSize", pageSize);
//		 map.put("teamName", teamName);
//		 return sqlSessionTemplate.selectList("getTeamsec",map);
//	}
//
//	@Override
//	public List<Map<String, Object>> getTeamsecc(String teamName) throws Exception {
//		// TODO Auto-generated method stub
//		List<Map<String, Object>> list=sqlSessionTemplate.selectList("getTeamsecc",teamName);
//		return list;
//	}

	

	
	
	
	






	


}
